﻿<html>
<title>Phân số</title>
<body bgcolor=yellow text=blue>
<form method="post" action="">
 <center><h1><b><font color="blue">PHÂN SỐ<hr width =30%></font></h1></u>
  <br>
		Nhập vào số 1: 
		<br>
		Tử số 1:
  <input type="text" name="tu1" size="5">
  <br>
  Mẫu số 1:
  <input type="text" name="mau1" size="5">
  <br>
  Nhập vào số 2:
  <br>
  Tử số 2:
  <input type="text" name="tu2" size="5">
  <br>
  Mẫu số 2:
  <input type="text" name="mau2" size="5">
  <br>
  <input type="submit" name="text" value="Xem">
  <input type="reset" name="text" value= "  Xóa ">
  <br><br>
  <hr width=30%></body><br><br>
  </form>
</html>
<?php
	if(isset($_POST["tu1"])&&isset($_POST["mau1"])&&isset($_POST["tu2"])&&isset($_POST["mau2"]))
{  
   $tu1=$_POST["tu1"];
   $tu2=$_POST["tu2"];
   $mau1=$_POST["mau1"];
   $mau2=$_POST["mau2"];
	$num1=is_numeric($tu1);
	$num2=is_numeric($tu2);
	$num3=is_numeric($mau1);
	$num4=is_numeric($mau2);	
     if($tu1==null ||$tu2==null ||$mau1==null ||$mau2==null || $num1!=1|| $num2!=1|| $num4!=1|| $num4!=1)
	   {
		  echo "Lỗi!";
		  return;
	   }
class phanso
{
	public $tu1;
	public $mau1;
	public $tu2;
	public $mau2;
	
	function __construct($tu1,$mau1,$tu2,$mau2)
	{
		$this->tu1=$tu1;
		$this->tu2=$tu2;
		$this->mau1=$mau1;
		$this->mau2=$mau2;
	}

	function xuat()
	{
		echo "Phân số 1: ".$this->tu1."/".$this->mau1."<br>";
		echo "Phân số 2: ".$this->tu2."/".$this->mau2."<br>";
	}
	function cong()
	{
		echo " Cộng: ";
		if($this->mau1==$this->mau2)
			echo $this->tu1+$this->tu2."/".$this->mau1;
			else
			echo ($this->tu1)*($this->mau2)+($this->tu2)*($this->mau1)."/".$this->mau1*$this->mau2;
			
	}
	function tru()
	{
		echo " <br>Trừ: ";
		if($this->mau1==$this->mau2)
			echo $this->tu1-$this->tu2."/".$this->mau1;
			else
			echo ($this->tu1)*($this->mau2)-($this->tu2)*($this->mau1)."/".$this->mau1*$this->mau2;
	}
	function nhan()
	{
		echo " <br>Nhân: ";
		echo $this->tu1*$this->tu2."/";
		echo $this->mau1*$this->mau2;
	}
	function chia()
	{
		echo "<br> Chia: ";
		echo $this->tu1*$this->mau2."/";
		echo $this->mau1*$this->tu2;
	}
	}
	
	$ps= new phanso($tu1,$mau1,$tu2,$mau2);
	$ps->xuat();
	$ps->cong();
	$ps->tru();
	$ps->nhan();
	$ps->chia();
	
}

